package com.penghaisoft.microwms.material.web.outwarehouse.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.RawRequisitionSn;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.util.ExcelExpUtil;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IBaseDictionaryService;
import com.penghaisoft.microwms.material.web.outwarehouse.service.OutWhRawSnService;

/**
 * Created by 王治国 on 2018/5/17.
 * 原材料出库查询
 */
@RestController
@RequestMapping(value = "raw/rawOutWhSn")
@Slf4j
public class OutWhRawSnController extends BaseController {

    @Autowired
    private OutWhRawSnService outWhRawSnService;
	@Autowired
	private IBaseDictionaryService baseDictionaryService;

    @PostMapping("list")
    public TransResult findAll(@RequestParam(name = "page", defaultValue = "1") int page,
                               @RequestParam(name = "rows", defaultValue = "10") int rows, RawRequisitionSn rawRequisitionSn) {
        Pager<RawRequisitionSn> result = outWhRawSnService.findAll(page, rows, rawRequisitionSn);
        
        String sumInfo = result.getOrder();
		if (sumInfo != null && sumInfo.length() > 0) {
			result.setOrder(sumInfo);
		}
		
        return success(result);
    }
    
    /** 
    * @Title: rawRequisitionSnExp 
    * @Description: 原材料领料出库明细导出
    * @author jzh 
    * @throws 
    */
    @GetMapping("rawRequisitionSnExp")
	public String rawRequisitionSnExp(RawRequisitionSn rawRequisitionSn, HttpServletRequest request, HttpServletResponse response){
		
		List<RawRequisitionSn> dataList = outWhRawSnService.queryRequisitionSnExp(rawRequisitionSn);
		BaseDictionary baseDictionary1 = new BaseDictionary();
		baseDictionary1.setActiveFlag("1");
		baseDictionary1.setDicType("model_type");
		List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);
		if (dataList != null && !dataList.isEmpty()) {
			for (RawRequisitionSn tmpData : dataList) {
				if (list1 != null && !list1.isEmpty()) {
					for (BaseDictionary tmp : list1) {
						// 判断产品类型
						if (tmp.getDicCode().equals(tmpData.getModelType())) {
							tmpData.setModelType(tmp.getDicName());
						}
					}
				}
			}
		}
		
		List<Map> columnList = new ArrayList<Map>();
		Map<String,String> columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "领料单号");
		columnMap.put("columnBeanName", "requisitionNo");
		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "合同编号");
		columnMap.put("columnBeanName", "contractNo");
		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "条码");
		columnMap.put("columnBeanName", "barcode");
		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "型号");
		columnMap.put("columnBeanName", "model");
		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "型号描述");
		columnMap.put("columnBeanName", "modelDesc");
		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "型号类型");
		columnMap.put("columnBeanName", "modelType");
		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "单位");
		columnMap.put("columnBeanName", "unit");
		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "数量");
		columnMap.put("columnBeanName", "amount");
		columnList.add(columnMap);
//		columnMap = new HashMap<String,String>();
//		columnMap.put("columnComment", "状态");
//		columnMap.put("columnBeanName", "barcodeStatus");
//		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "扫描日期");
		columnMap.put("columnBeanName", "gmtCreate");
		columnList.add(columnMap);
		columnMap = new HashMap<String,String>();
		columnMap.put("columnComment", "扫描人");
		columnMap.put("columnBeanName", "createBy");
		columnList.add(columnMap);
		
		SXSSFWorkbook sw = ExcelExpUtil.generateWorkbook(dataList, columnList);
		
		OutputStream out = null;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String sdfDate = sdf.format(date);
		String fileName = "原材料领料出库明细"+sdfDate+".xlsx";// 文件名
		try {
			out = response.getOutputStream();
			response.setContentType("application/force-download");// 设置强制下载不打开
			response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));
			sw.write(out);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	return "sucess";
	}

}
